Skip to content

feat(cache): add cache token audit logging behind OPENCODE_EXPERIMENTAL_CACHE_AUDIT#27007

Closed
martinffx wants to merge 2 commits into
anomalyco:devfrom
martinffx:feat/cache-audit-logging
Closed

feat(cache): add cache token audit logging behind OPENCODE_EXPERIMENTAL_CACHE_AUDIT#27007
martinffx wants to merge 2 commits into
anomalyco:devfrom
martinffx:feat/cache-audit-logging

Conversation

@martinffx

Copy link
Copy Markdown

Issue for this PR

Closes #5416, #5224
Related: #14065, #5422, #14203, #14743

Part of the stacked PR decomposition of the prompt caching fix (1 of 4). Adds cache token audit logging as observability foundation, the metrics logged here are what we use to verify cache hit improvements in subsequent PRs.

Type of change

  • New feature

What does this PR do?

Adds cache token audit logging behind OPENCODE_EXPERIMENTAL_CACHE_AUDIT=1, providing per-LLM-call cache hit/miss visibility. This PR is purely observability — it does not change any caching behavior.

Changes:

  1. packages/core/src/flag/flag.ts — adds OPENCODE_EXPERIMENTAL_CACHE_AUDIT flag.

  2. packages/opencode/src/session/session.ts — adds rawInputTokens to the usage return value (sum of input + cache read + cache write), needed by both the log and sidebar display.

  3. packages/opencode/src/session/processor.ts — after each LLM call's getUsage(), if the flag is set, logs a [CACHE] line with total input tokens, cache read/write/new breakdown, hit percentage, and output tokens.

  4. packages/opencode/src/cli/cmd/tui/feature-plugins/sidebar/context.tsx — when the flag is set, displays a "Cache Audit" box in the TUI sidebar showing the same token breakdown and hit rate in real time.

Why this PR first: The subsequent PRs in the stack improve cache hit rates. This PR provides the measurement tooling to verify those improvements work.

How did you verify your code works?

Set OPENCODE_EXPERIMENTAL_CACHE_AUDIT=1 bun dev, send a prompt, and confirm:

  • A [CACHE] log line appears per LLM call in the log output
  • The sidebar shows the "Cache Audit" box with token breakdown

Tests: bun test packages/opencode/test/session/session.test.ts passes.

Screenshots

Screenshot 2026-05-12 at 08 50 19

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Co-authored-by: Bhagirathsinh Vaghela bhagirathsinh.vaghela.001@gmail.com

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:title labels May 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hey! Your PR title Feat/cache audit logging doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@martinffx martinffx changed the title Feat/cache audit logging feat(cache): add cache token audit logging behind OPENCODE_EXPERIMENTAL_CACHE_AUDIT May 12, 2026
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label May 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

bhagirathsinh-vaghela and others added 2 commits May 17, 2026 14:24
@martinffx martinffx force-pushed the feat/cache-audit-logging branch from f6b1ccd to c186e10 Compare May 18, 2026 06:33
@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Anthropic (and others) caching improvement

2 participants